//
//  NSString+Navinfo.m
//  AIWAYS
//
//  Created by Marx on 2018/6/15.
//  Copyright © 2018年 wlq. All rights reserved.
//

#import "NSString+Navinfo.h"

@implementation NSString (Navinfo)


-(BOOL)conformToRulesForPassWord{
    BOOL result = false;
    if ([self length] >= 6){
        // 判断长度大于6位后再接着判断是否同时包含数字和字符
        NSString * regex = @"^(?![0-9]+$)(?![a-zA-Z]+$)[0-9A-Za-z]{6,12}$";
        NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@", regex];
        result = [pred evaluateWithObject:self];
    }
    return result;
}



//根据正则，过滤汉字
- (NSString *)filterCharactor{
    // 准备对象
    NSString * regExpStr = @"[\u4e00-\u9fa5]";
    NSString * replacement = @"";
    // 创建 NSRegularExpression 对象,匹配 正则表达式
    NSRegularExpression *regExp = [[NSRegularExpression alloc] initWithPattern:regExpStr options:NSRegularExpressionCaseInsensitive error:nil];
    NSString *resultStr = self;
    // 替换匹配的字符串
    resultStr = [regExp stringByReplacingMatchesInString:self
                                                 options:NSMatchingReportProgress
                                                   range:NSMakeRange(0, self.length)
                                            withTemplate:replacement];
    
    return resultStr;
}

- (BOOL) inputHasChinese

{
    
    NSString *regex = @"[\u4e00-\u9fa5]+";
    
    NSPredicate *pred = [NSPredicate predicateWithFormat:@"SELF MATCHES %@",regex];
    
    if ([pred evaluateWithObject:self]) {
        
        return YES;
        
    }
    
    return NO;
    
}


//根据正则，过滤除数字字母外的符号
- (NSString *)filterCharactorForPassword{
    
    NSString *searchText = self;
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9A-Za-z]" options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *result = [regex stringByReplacingMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange(0, searchText.length) withTemplate:@""];
    return result;
}

-(NSString *)filterCharactorForNickname
{
   NSString *searchText = self;
    NSError *error = NULL;
    NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:@"[^0-9A-Za-z\u4e00-\u9fa5]" options:NSRegularExpressionCaseInsensitive error:&error];
    NSString *result = [regex stringByReplacingMatchesInString:searchText options:NSMatchingReportCompletion range:NSMakeRange(0, searchText.length) withTemplate:@""];
    return result;
}

- (BOOL) isEmpty{
    
    if (!self || [self isEqual:@""]) {
        
        return true;
        
    } else {
        
        NSCharacterSet *set = [NSCharacterSet whitespaceAndNewlineCharacterSet];
        
        NSString *trimedString = [self stringByTrimmingCharactersInSet:set];
        
        if ([trimedString length] == 0) {
            
            return true;
            
        } else {
            
            return false;
            
        }
        
    }
    
}

/**
 手机号中间四位加密
 
 @return 加密后的手机号
 */
-(NSString *)telephoneNumberIntermediateFourBitEncryption
{
    if (self.length >= 7) {
        
        NSString *numberString = [self stringByReplacingCharactersInRange:NSMakeRange(3, 4) withString:@"****"];
        
        return numberString;
    }
    else
    {
        return self;
    }
}

/**
 身份证号加密
 
 @return 加密后的身份证号
 */
-(NSString *)idCardIntermediateFourBitEncryption
{
    if (self.length >= 4) {
        
        NSString *str = [self stringByReplacingCharactersInRange:NSMakeRange(2, self.length - 4) withString:@"**************"];
        
        return str;
    }
    else
    {
        return self;
    }
}

/**
 vin加密
 
 @return 加密后的vin
 */
-(NSString *)vinEncryption
{
    if (self.length >= 4) {
        
        NSString *str = [self stringByReplacingCharactersInRange:NSMakeRange(self.length - 4, 4) withString:@"****"];
        
        return str;
    }
    else
    {
        return @"****";
    }
}

/**
 其他号码加密
 
 @return 加密后的字符串
 */
-(NSString *)otherIDEncryption
{
    if (self.length >= 4) {
        
        NSString *str = [self stringByReplacingCharactersInRange:NSMakeRange(self.length - 4, 4) withString:@"****"];
        
        return str;
    }
    else
    {
        return @"****";
    }
}

- (NSInteger)validStrlength
{
    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);
    NSData* data = [self dataUsingEncoding:enc];
    return [data length];
}



@end
